using System.Text.RegularExpressions;

namespace CSharpPro2._1016;

using System.Collections.Generic;
using System.Text.RegularExpressions; 

public class User
{

    public class TestPrint2
    {
        public static void Test7()
        {
            Dictionary<string, string> users = new Dictionary<string, string>();
            
            while (true)
            {
                Console.WriteLine("\n===== 主菜单 =====");
                Console.WriteLine("1. 登录");
                Console.WriteLine("2. 注册");
                Console.WriteLine("3. 退出");
                Console.Write("请选择功能（1-3）：");
                
                string choice = Console.ReadLine()?.Trim();
                switch (choice)
                {
                    case "1":
                        Login(users);
                        break;
                    case "2":
                        Register(users);
                        break;
                    case "3":
                        Console.WriteLine("系统已退出，再见！");
                        return;
                    default:
                        Console.WriteLine("输入错误，请输入1-3之间的数字！");
                        break;
                }
            }
        }

        private static void Register(Dictionary<string, string> users)
        {
            Console.WriteLine("\n===== 用户注册 =====");
            Console.Write("请输入用户名：");
            string username = Console.ReadLine()?.Trim();
            if (string.IsNullOrWhiteSpace(username))
            {
                Console.WriteLine("注册失败：用户名不可为空！");
                return;
            }

            var (isValid, msg) = IsValidUsername(username);
            if (!isValid)
            {
                Console.WriteLine($"注册失败：{msg}");
                return;
            }
            if (users.ContainsKey(username))
            {
                Console.WriteLine("注册失败：用户名已存在！");
                return;
            }

            Console.Write("请输入密码：");
            string password = Console.ReadLine()?.Trim();
            if (string.IsNullOrWhiteSpace(password))
            {
                Console.WriteLine("注册失败：密码不可为空！");
                return;
            }

            (isValid, msg) = IsValidPassword(password);
            if (!isValid)
            {
                Console.WriteLine($"注册失败：{msg}");
                return;
            }

            Console.Write("请确认密码：");
            string confirmPwd = Console.ReadLine()?.Trim();
            if (string.IsNullOrWhiteSpace(confirmPwd))
            {
                Console.WriteLine("注册失败：确认密码不可为空！");
                return;
            }
            if (password != confirmPwd)
            {
                Console.WriteLine("注册失败：两次输入的密码不一致！");
                return;
            }

            users.Add(username, password);
            Console.WriteLine("注册成功！请返回登录");
        }
        private static void Login(Dictionary<string, string> users)
        {
            Console.WriteLine("\n===== 用户登录 =====");
            
            Console.Write("请输入用户名：");
            string username = Console.ReadLine()?.Trim();
            if (string.IsNullOrWhiteSpace(username))
            {
                Console.WriteLine("登录失败：用户名不可为空！");
                return;
            }
            
            Console.Write("请输入密码：");
            string password = Console.ReadLine()?.Trim();
            if (string.IsNullOrWhiteSpace(password))
            {
                Console.WriteLine("登录失败：密码不可为空！");
                return;
            }
            
            if (!users.ContainsKey(username))
            {
                Console.WriteLine("登录失败：用户名不存在！");
            }
            else if (users[username] != password)
            {
                Console.WriteLine("登录失败：密码错误！");
            }
            else
            {
                Console.WriteLine($"登录成功！欢迎回来，{username}！");
                Console.WriteLine("已进入系统首页（模拟）");
            }
        }

        private static (bool, string) IsValidUsername(string username)
        {
            if (username.Length < 3 || username.Length > 15)
            {
                return (false, "用户名长度必须为3-15个字符");
            }
            if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
            {
                return (false, "用户名只能包含字母、数字和下划线");
            }
            return (true, "");
        }

        private static (bool, string) IsValidPassword(string password)
        {
            if (password.Length < 6 || password.Length > 20)
            {
                return (false, "密码长度必须为6-20个字符");
            }
            if (!Regex.IsMatch(password, @"[A-Z]"))
            {
                return (false, "密码必须包含至少1个大写字母");
            }
            if (!Regex.IsMatch(password, @"[a-z]"))
            {
                return (false, "密码必须包含至少1个小写字母");
            }
            if (!Regex.IsMatch(password, @"[0-9]"))
            {
                return (false, "密码必须包含至少1个数字");
            }
            return (true, "");
        }
    }
}
